package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.pojo.NursingLevel;
import com.zzyl.nursing.domain.vo.NursingLevelVo;
import com.zzyl.nursing.domain.vo.TreeVo;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.INursingLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 * 护理等级Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
//        return nursingLevelMapper.insert(nursingLevel);
        //先更新数据库,再删除缓存
        int insert = nursingLevelMapper.insert(nursingLevel);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return insert;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
//        return nursingLevelMapper.updateById(nursingLevel);
        int deleted = nursingLevelMapper.insert(nursingLevel);
        redisTemplate.delete(nursingLevel.getId());
        return deleted;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        return nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        return nursingLevelMapper.deleteById(id);
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    @Override
    public List<NursingLevelVo> listLevel() {
        return List.of();
    }

    @Override
    public List<? extends Object> listAll() {


//        LambdaQueryWrapper<NursingLevel> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(NursingLevel::getStatus, 1);
//        List<NursingLevel> list = list(queryWrapper);
//        return list;

        //1.如果缓存中有,则返回直接返回
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        if (redisTemplate.hasKey(CACHE_KEY_PREFIX)) {
            return (List<NursingLevel>) ops.get(CACHE_KEY_PREFIX);
        }
        //2.如果缓存中没有,则从数据库中查询
        //1.查询所有等级
        List<NursingLevel> nursingLevelList =this.lambdaQuery().eq(NursingLevel::getStatus, 1).list();
        //2.转换对象NursingLevelVo
        List<NursingLevelVo> nursingLevelVoList = BeanUtil.copyToList(nursingLevelList, NursingLevelVo.class);
        //3.查询并放入缓存中
        ops.set( CACHE_KEY_PREFIX, JSONUtil.toJsonStr(nursingLevelVoList));
        return nursingLevelVoList;

    }

    @Override
    public List<TreeVo> getRoomAndBedByBedStatus(Integer status) {

        return List.of();
    }

//查询

}
